package org.stvd.controller.workflow.leave;

import java.util.List;
import java.util.Map;

import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.stvd.common.Consts;
import org.stvd.common.DepAndUserHolder;
import org.stvd.common.security.SecurityUserHolder;
import org.stvd.common.utils.ServiceResult;
import org.stvd.common.workflow.enums.WorkflowApplyStatusEnum;
import org.stvd.common.workflow.enums.WorkflowApproveStatusEnum;
import org.stvd.controller.workflow.ApproveBaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.Users;
import org.stvd.entities.workflow.leave.BsLeaveInfo;
import org.stvd.service.admin.UsersService;
import org.stvd.service.common.CodeTableService;
import org.stvd.service.workflow.leave.BsLeaveInfoService;

/**
 * @Title BsLeaveApproveController
 * @Destribution 请假申请审批控制类
 * @author fenglong
 * @date 2020年1月20日
 */
@Controller
@RequestMapping("/workflow/leave/approve")
public class BsLeaveApproveController extends ApproveBaseController<BsLeaveInfo> {

    @Autowired
    private BsLeaveInfoService leaveInfoService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private CodeTableService codeTableService;
 
    private String tmpURL = "/workflow/leave/approve/";
 
    @Override
    protected String loadPageManageData(BsLeaveInfo bsLeaveInfo) {
        map.put("bsLeaveInfo", bsLeaveInfo);
        map.put("userId", SecurityUserHolder.getCurrentUserId());
        QueryResult<Map<String, Object>> queryResult = leaveInfoService.queryLeaveApproResult((pageNo - 1) * pageSize,
                pageSize, bsLeaveInfo.getAuditStatus(),SecurityUserHolder.getCurrentUserId());
        List<Users> users = usersService.findUsers("", "", "", Consts.GUID);
        map.put("users", users);
        map.put("queryResult", queryResult);
        map.put("applyStatusMap", WorkflowApplyStatusEnum.applyStatusMap);
        map.put("approveStatusMap", WorkflowApproveStatusEnum.approveStatusMap);
        return tmpURL + "LeaveManage.html";
    }
 
    @Override
    protected String loadPageClaimData(String processInstanceId, String taskId) {
        BsLeaveInfo bsLeaveInfo = leaveInfoService.findByProcessId(processInstanceId);
        if (bsLeaveInfo != null) {
            if(bsLeaveInfo.getLeaveCategory().equals("1")) {
                bsLeaveInfo.setLeaveCategory("按小时请");
            }else {
                bsLeaveInfo.setLeaveCategory("按天请");
            }
            try {
                for (int i = 0; i < codeTableService.findAll("dic_leave_type").size(); i++) {
                    if (bsLeaveInfo.getLeaveType()
                            .equals(codeTableService.findAll("dic_leave_type").get(i).getId())) {
                        bsLeaveInfo.setLeaveType(codeTableService.findAll("dic_leave_type").get(i).getName());
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            map.put("bsLeaveInfo", bsLeaveInfo);
            map.put("applyStatusMap", WorkflowApplyStatusEnum.applyStatusMap);
        }
        return tmpURL + "LeaveClaim.html";
    }
 
    @Override
    protected String loadPageApproveData(Task task) {
        if (task != null) {
            BsLeaveInfo bsLeaveInfo = leaveInfoService.findByProcessId(task.getProcessInstanceId());
            if (bsLeaveInfo != null) {
                if(bsLeaveInfo.getLeaveCategory().equals("1")) {
                    bsLeaveInfo.setLeaveCategory("按小时请");
                }else {
                    bsLeaveInfo.setLeaveCategory("按天请");
                }
                try {
                    for (int i = 0; i < codeTableService.findAll("dic_leave_type").size(); i++) {
                        if (bsLeaveInfo.getLeaveType()
                                .equals(codeTableService.findAll("dic_leave_type").get(i).getId())) {
                            bsLeaveInfo.setLeaveType(codeTableService.findAll("dic_leave_type").get(i).getName());
                        }
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                map.put("bsLeaveInfo", bsLeaveInfo);
                map.put("applyStatusMap", WorkflowApplyStatusEnum.applyStatusMap);
            }
        }
        return tmpURL + "LeaveApprove.html";
    }
 
    @Override
    protected String loadPageJumpData(Task task) {
        return null;
    }
 
    @Override
    protected ServiceResult<Map<String, Object>> handlerClaim(String processInstanceId, String taskId,
            Map<String, Object> variables) {
        return leaveInfoService.claimLeave(processInstanceId);
    }
 
    @Override
    protected ServiceResult<Map<String, Object>> handlerRefuse(String processInstanceId, String taskId,
            String jumpTaskKey, Map<String, Object> variables) {
        return leaveInfoService.refuseLeave(processInstanceId,jumpTaskKey, variables);
    }
 
    @Override
    protected ServiceResult<Map<String, Object>> handlerPass(String processInstanceId, String taskId,
            Map<String, Object> variables) {
        // TODO Auto-generated method stub
        return leaveInfoService.approveLeave(processInstanceId, variables);
    }
 
    @Override
    protected ServiceResult<Map<String, Object>> handlerJump(String processInstanceId, String taskId,
            String jumpTaskKey, Map<String, Object> variables) {
        // TODO Auto-generated method stub
        return null;
    }
 
    @Override
    protected String processDetail(String processInstanceId) {
        BsLeaveInfo bsLeaveInfo = leaveInfoService.findByProcessId(processInstanceId);
        if (bsLeaveInfo != null) {
            if(bsLeaveInfo.getLeaveCategory().equals("1")) {
                bsLeaveInfo.setLeaveCategory("按小时请");
            }else {
                bsLeaveInfo.setLeaveCategory("按天请");
            }
            try {
                for (int i = 0; i < codeTableService.findAll("dic_leave_type").size(); i++) {
                    if (bsLeaveInfo.getLeaveType()
                            .equals(codeTableService.findAll("dic_leave_type").get(i).getId())) {
                        bsLeaveInfo.setLeaveType(codeTableService.findAll("dic_leave_type").get(i).getName());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            map.put("bsLeaveInfo", bsLeaveInfo);
            map.put("applyStatusMap", WorkflowApplyStatusEnum.applyStatusMap);
        }
        return tmpURL + "LeaveDetail.html";
    }
 
    @Override
    protected String loadActivityData(String processInstanceId, String jumpTaskKey) {
        BsLeaveInfo bsLeaveInfo = leaveInfoService.findByProcessId(processInstanceId);
            String userAssignee = "";
            if(bsLeaveInfo != null) {
                if(!StringUtil.isEmpty(bsLeaveInfo.getAuditUid())) {
                    userAssignee = bsLeaveInfo.getAuditUid();
                }
            }
            return DepAndUserHolder.getTargetTaskUids(userAssignee,jumpTaskKey);
    }

}
